$('.addspecial').click(function(){
    $('.special').append($(".setspecial option:selected").clone());
})
/* $('.addvis').click(function(){
    $(this).parents('tr')
}) */

var V=new Vue({
    el:'#main',
    data:()=>({
        dataval:[
            {
                proname:'',//产品名称
                proint:'',//简介
                procalss:[],
                datvis:[    //行
                    {
                        model:'',//型号
                        factory_model:'',
                        package_qty:'',//包装
                        qty:1,//数量
                        ctn_size:'',//尺寸
                        ctn_kgs:'',//重量
                        ctn_note:'',//说明
                        codebar:'',//条码    
                        symbol:[
                            // {
                            //     selval:0,
                            //     value:[{
                            //         name:'name',
                            //         val:1
                            //     }],
                            //     key:'key'
                            // }
                        ]
                    }
                ]
            },
            {
                // proclalist:[],//产品分类
                proname:'',//产品名称
                proint:'',//简介
                procalss:[],
                datvis:[    //行
                    {
                        model:'',//型号
                        factory_model:'',
                        package_qty:'',//包装
                        qty:1,//数量
                        ctn_size:'',//尺寸
                        ctn_kgs:'',//重量
                        ctn_note:'',//说明
                        codebar:'',//条码    
                        symbol:[
                            // {
                            //     selval:0,
                            //     value:[{
                            //         name:'name',
                            //         val:1
                            //     }],
                            //     key:'key'
                            // }
                        ]
                    }
                ]
            },
            {
                // proclalist:[],//产品分类
                proname:'',//产品名称
                proint:'',//简介
                procalss:[],
                datvis:[    //行
                    {
                        model:'',//型号
                        factory_model:'',
                        package_qty:'',//包装
                        qty:1,//数量
                        ctn_size:'',//尺寸
                        ctn_kgs:'',//重量
                        ctn_note:'',//说明
                        codebar:'',//条码    
                        symbol:[
                            // {
                            //     selval:0,
                            //     value:[{
                            //         name:'name',
                            //         val:1
                            //     }],
                            //     key:'key'
                            // }
                        ]
                    }
                ]
            }
        ],
        brand:[],
        brandall:[],
        brandval:0, //品牌的值
        ncm:'',//海关编号
        productCate:[], //产品分类的值

        procate:[],//被选中的产品分类值
        productBrand:[], //品牌
        lang:'',
        cheradioval:1,  //状态
        isfei:1,  //是否可售
        isgeneral:2,
        showcat:'',
        title:'',
        attrarr:{},//待选特征
        seladdsttr:0,
        alllangeclass:{},
        attris_show:true
    }),
    methods:{
        getdata(){  //获取数据方法
            var data={
                lang:this.lang,
            }
            if($('#type').val()=='edit'){
                var geturl="/admin/product/productEdit?request=edit&id="+$('#did').val()
            }else{
                var geturl="/admin/product/productAdd?request=add"  
            }
            $.ajax({
                url:geturl,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.title=data.title   //标题
                    this.alllangeclass=data.product_cate_data  //产品分类
                    this.productCate=data.product_cate_data.zh//
                    this.brand=data.product_brand_data.zh
                    this.brandall=data.product_brand_data  //品牌
                    this.showcat=""
                    this.attrarr=data.profeature   //特征
                    if(!this.attrarr.zh.length){
                        this.attris_show=false
                    }
                    for (const key in this.attrarr) {
                        this.attrarr[key].forEach(element => {
                            element.hasadd=false    
                        });
                    }
                    if($('#type').val()=="add"){
                        var newbarcode=''
                        $.ajax({
                            url:'/admin/api/getProcharCodebar',
                            type:'post',
                            success:(data)=>{
                                newbarcode=data
                                this.dataval.map(item=>{
                                    item.datvis[0].codebar=newbarcode
                                })
                            }
                        })
                    }
                    //获得编辑的数据
                    if($('#type').val()=='edit'){
                        this.procate=data.procate
                        this.procate.forEach(element => {
                            this.productCate.forEach(item => {
                                if(item.id==element){
                                    if(this.showcat.length){
                                        this.showcat+='/'
                                        this.showcat+=item.content
                                    }else{
                                        this.showcat=item.content
                                    }
                                }
                            });   
                        });
                        data.prochar.zh[0].symbol.forEach(item => {
                            for (const key in this.attrarr) {
                                this.attrarr[key].forEach(element => {
                                    if(element.id==item.id)
                                    element.hasadd=true
                                });
                            }
                        });
                        if(this.attrarr.zh.every(element=>{
                            return element.hasadd
                        })){
                            this.attris_show=false
                        }else{
                            this.attris_show=true
                        }   
                        this.dataval.map((item,index)=>{
                            switch (index){
                                case 0:
                                    item.datvis=data.prochar.zh
                                    item.proname=data.proinfo.zh.name
                                    item.proint=data.proinfo.zh.prodesc
                                    break;
                                case 1:
                                    item.datvis=data.prochar.en
                                    item.proname=data.proinfo.en.name
                                    item.proint=data.proinfo.en.prodesc
                                    break;
                                case 2:
                                    item.datvis=data.prochar.pt
                                    item.proname=data.proinfo.pt.name
                                    item.proint=data.proinfo.pt.prodesc
                                    break;
                                
                            }
                            this.brandval=data.proinfo.zh.brand_id
                            this.ncm=data.proinfo.zh.ncm
                        })
                    }
                }
            })
        },
        subfn(){
            var data={
                cateval:this.procate,//产品分类
                brandval:this.brandval,//品牌的值
                statue:this.cheradioval,//状态值
                isfei:this.isfei,//状态值
                isgeneral:this.isgeneral,//状态值
                ncm:this.ncm,//状态值
                zh:this.dataval[0],//中文值
                en:this.dataval[1],//英文值
                pt:this.dataval[2],//葡文值
            }
            this.productInsert(data)
        },
        productInsert(data){
            if($('#type').val()=='edit'){
                var url="/admin/product/productEdit?request=update&id="+$('#did').val()
            }else{
                var url="/admin/product/productAdd?request=insert"  
            }
            $.ajax({
                url:url,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if(data.status==1){
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        $('#fanhui').attr('noSave','true')
                        this.getdata()
                    }else{
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        $('#fanhui').attr('noSave','false')
                    }
                }
            }) 

        },
        setTzfun(index){
            if(this.seladdsttr){
                $.ajax({
                    url:'/admin/product/productAdd?request=add&getcontent=feature&id='+this.seladdsttr,
                    type:'post',
                    success:(data)=>{
                        data = JSON.parse(data)
                        var newattr=[{key:'',id:'',selval:0,value:[]},{key:'',id:'',selval:0,value:[]},{key:'',id:'',selval:0,value:[]}]
                        data.data.forEach(element => {
                            newattr[0].key=element.featureName_zh
                            newattr[1].key=element.featureName_en
                            newattr[2].key=element.featureName_pt
                            newattr[0].id=element.feature_id
                            newattr[1].id=element.feature_id
                            newattr[2].id=element.feature_id
                            newattr[0].value.push({name:element.name_zh,val:element.id})
                            newattr[1].value.push({name:element.name_en,val:element.id})
                            newattr[2].value.push({name:element.name_pt,val:element.id})
                        });
                        this.dataval.map((element,index)=>{
                            element.datvis.map(item=>{
                                item.symbol.push(JSON.parse(JSON.stringify(newattr[index])))
                            })
                        })
                        for (const key in this.attrarr) {
                            this.attrarr[key].forEach(element => {
                                if(element.id=this.seladdsttr){
                                    element.hasadd=true
                                }
                            });
                        }
                        if(this.attrarr.zh.every(element=>{
                            return element.hasadd
                        })){
                            this.attris_show=false
                        }
                        this.seladdsttr=0
                    }
                })
            }
        },
        addvis(lang,index){ //添加一行
            var barcode=''
            $.ajax({
                url:'/admin/api/getProcharCodebar',
                type:'post',
                success:(data)=>{
                    barcode=data
                    this.dataval.map(item=>{
                        var data=JSON.parse(JSON.stringify(item.datvis[index]))
                        data.codebar=barcode
                        item.datvis.push(data)
                    })
                }
            })
        },
        delvis(lang,index){  //删除行
            if(this.dataval[lang].datvis.length>1)
            this.dataval[lang].datvis.splice(index,1)
        },
        changelange(lang){
            this.lang=lang
        },
        changeradio(val){
            this.cheradioval=val
        },
        changefei(val){
            this.isfei=val
        },
        enter(){
            this.showcat=""
            this.procate=[]
            $('.formbox').serializeArray().forEach(element => {
                element.value=element.value.split('/')
                if(this.procate.indexOf(element.value[0])){
                      if(this.showcat.length){
                            this.showcat+='/'
                            this.showcat+=element.value[1]
                        }else{
                            this.showcat=element.value[1]
                        }
                    this.procate.push(element.value[0])
                }
                      
            });
            $('#procat_setModal').modal('toggle')
        },
        changeselval(row,index,event){
            this.dataval.map(item=>{
                item.datvis[row].symbol[index].selval=event.target.value
            })
        },
        delclass(index){
            var id
            this.dataval.map((item)=>{
                item.datvis.map((item)=>{
                    id=item.symbol[index].id
                    item.symbol.splice(index,1)
                })
            })
            for (const key in this.attrarr) {
                this.attrarr[key].forEach(element => {
                    if(element.id=id)
                    element.hasadd=false
                });
            }
            if(this.attrarr.zh.every(element=>{
                return element.hasadd
            })){
                this.attris_show=false
            }else{
                this.attris_show=true
            }
        },
        fanhui(){
                window.location.href = "/admin/product/productlist";
        },
        changeval(index,key,e){
            this.dataval.map((element)=>{
                element.datvis[index][key]=e.target.value
            })
        }
    },
    mounted(){
        this.getdata()
        $('#procat_setModal').on('show.bs.modal', (e)=> {
        })
    },
    watch:{
        lang:function(){
            this.productCate=this.alllangeclass[this.lang]
            this.brand=this.brandall[this.lang]
        }
    }
})